import lombok.extern.slf4j.Slf4j;

import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author cz
 * RegionServer start point, invoke zk manager to connect with zk
 * and connect with ftp
 */
@Slf4j
public class RegionServer {
    /**
     * CLIENT_PORT for client connection
     * FAILURE_PORT for backup connection
     */
    static final int CLIENT_PORT = 8001;
    static final int FAILURE_PORT = 8010;

    public static void main(String[] args) {
        try {
            //make region server a server for clients
            ServerSocket serverSocket = new ServerSocket(CLIENT_PORT);
            //init miniSQL
            API.initial();
            FTPConnector.connectFTP();
            ZookeeperManager.zookeeperConnect();
            Thread failureThread = new BackupThread();
            failureThread.start();
            // use multi-thread to connect with clients
            while (true) {
                Socket socket = serverSocket.accept();
                Thread regionThread = new RegionClientThread(socket);
                regionThread.start();
            }
        } catch (Exception e) {
            log.warn("REGION_SERVER start failed!");
            if (ZookeeperManager.client != null) {
                ZookeeperManager.closeConnect();
            }
            e.printStackTrace();
        }
    }

}



